package com.yang.strategy.util;

import cn.hutool.dfa.WordTree;
import com.yang.common.constant.CacheConstant;
import com.yang.strategy.client.BeaconCacheClient;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author yangyongsheng
 * @date 2024/9/7 23:38
 * @description
 */
public class HutoolDFAUtil {
    //存档敏感词的敏感词树，WordTree是hutool封装的类
    private static WordTree wordTree = new WordTree();

    static {
        // 获取Spring容器中的cacheClient
        BeaconCacheClient cacheClient = (BeaconCacheClient) SpringUtil.getBeanByClass(BeaconCacheClient.class);
        // 获取存储在Redis中的全部敏感词
        Set<String> dirtyWords = cacheClient.smember(CacheConstant.DIRTY_WORD);
        // 调用WordTree的add方法，将dfaMap的敏感词树构建
        wordTree.addWords(dirtyWords);
    }

    public static List<String> getDirtyWord(String text){
        return wordTree.matchAll(text);
    }
}
